<?php
namespace app\controllers;

use app\common\corelib\LocalUpload;

use app\logic\PaperLogic;
use app\logic\TestLogic;
use yii;
//试题管理
class TestController extends BackendController
{
    //中文试题管理
    public function actionChiness()
    {
        $query = Yii::$app->request->get('form', []);
        $page = Yii::$app->request->get('page', 1);
        $testLogic = new TestLogic();
        $this->data['breadcrumbs'] = '中文试题管理';
        return $this->render('index', [
            'paper_id'=>1,
            'test' => $testLogic->getTest(1,$query, $page),
            'query' => $query
        ]);
    }
    //西文试题管理
    public function actionSpanish()
    {
        $query = Yii::$app->request->get('form', []);
        $page = Yii::$app->request->get('page', 1);
        $testLogic = new TestLogic();
        $this->data['breadcrumbs'] = '西文试题管理';
        return $this->render('index', [
            'paper_id'=>2,
            'test' => $testLogic->getTest(2,$query, $page),
            'query' => $query
        ]);
    }


    //添加试题
    public function actionAdd(){
        $testLogic = new TestLogic();
        if(!Yii::$app->request->post()){
            $paper_id = Yii::$app->request->get('paper_id', 0);
            if($paper_id==1)
                $this->data['breadcrumbs'] = ['试题管理' => U('test/chiness'), ' 添加试题' => ''];
            elseif($paper_id==2)
                $this->data['breadcrumbs'] = ['试题管理' => U('test/spanish'), ' 添加试题' => ''];
            else{
                $paperLogic = new PaperLogic();
                $paper = $paperLogic->getById($paper_id);
                if($paper['paper_type']==3){
                    $this->data['breadcrumbs'] = ['试题管理' => U('paper/naughty'), '编辑模板' => U(['paper/edit','paper_id'=>$paper_id]), ' 添加试题' => ''];
                }
                elseif($paper['paper_type']==4){
                    $this->data['breadcrumbs'] = ['试题管理' => U('paper/taotao'), '编辑模板' => U(['paper/edit','paper_id'=>$paper_id]), ' 添加试题' => ''];
                }
                elseif($paper['paper_type']==5){
                    $this->data['breadcrumbs'] = ['试题管理' => U('paper/yct'), '编辑模板' => U(['paper/edit','paper_id'=>$paper_id]), ' 添加试题' => ''];
                }
                elseif($paper['paper_type']==6){
                    $this->data['breadcrumbs'] = ['试题管理' => U('paper/hsk'), '编辑模板' => U(['paper/edit','paper_id'=>$paper_id]), ' 添加试题' => ''];
                }
                elseif($paper['paper_type']==7){
                    $this->data['breadcrumbs'] = ['试题管理' => U('paper/pronunciation'), '编辑模板' => U(['paper/edit','paper_id'=>$paper_id]), ' 添加试题' => ''];
                }
                elseif($paper['paper_type']==8){
                    $this->data['breadcrumbs'] = ['试题管理' => U('paper/children'), '编辑模板' => U(['paper/edit','paper_id'=>$paper_id]), ' 添加试题' => ''];
                }
                elseif($paper['paper_type']==9){
                    $this->data['breadcrumbs'] = ['试题管理' => U('paper/dele'), '编辑模板' => U(['paper/edit','paper_id'=>$paper_id]), ' 添加试题' => ''];
                }
            }
            $test_type = Yii::$app->request->get('test_type', 0);
            return $this->render('add_'.$test_type, [
                'paper_id'=>$paper_id,
                'theme' => '/assets/',
                'actionUrl' => U('test/add')
            ]);
        }else{
            set_time_limit(0);
            $params = Yii::$app->request->post('form');
            if($params['test_type']==1){
                if($_FILES['test_detail']['name']){
                    LocalUpload::go_upload_video($_FILES['test_detail']);
                    $uploadresult = '/uploads/'.LocalUpload::$filedir.'/'.LocalUpload::$filename;
                }else{
                    $uploadresult = '';
                }
                $logicRes = $testLogic->addTest($params,$uploadresult);
            }elseif($params['test_type']==2){
                $logicRes = $testLogic->addTest($params);
            }elseif($params['test_type']==3){
                $logicRes = $testLogic->addTest($params);
            }elseif($params['test_type']==4){
                if($_FILES['test_options_A']['name']){
                    LocalUpload::go_upload($_FILES['test_options_A']);
                    $uploadresult = '/uploads/'.LocalUpload::$filedir.'/'.LocalUpload::$filename;
                    $thumbs = LocalUpload::thumbs(100,100);
                    $test_options['A'] = $uploadresult;
                    LocalUpload::go_upload($_FILES['test_options_B']);
                    $uploadresult = '/uploads/'.LocalUpload::$filedir.'/'.LocalUpload::$filename;
                    $thumbs = LocalUpload::thumbs(100,100);
                    $test_options['B'] = $uploadresult;
                    LocalUpload::go_upload($_FILES['test_options_C']);
                    $uploadresult = '/uploads/'.LocalUpload::$filedir.'/'.LocalUpload::$filename;
                    $thumbs = LocalUpload::thumbs(100,100);
                    $test_options['C'] = $uploadresult;
                    LocalUpload::go_upload($_FILES['test_options_D']);
                    $uploadresult = '/uploads/'.LocalUpload::$filedir.'/'.LocalUpload::$filename;
                    $thumbs = LocalUpload::thumbs(100,100);
                    $test_options['D'] = $uploadresult;
                }
                $logicRes = $testLogic->addTest($params,json_encode($test_options));
            }elseif($params['test_type']==5){
                if($_FILES['test_detail']['name']){
                    LocalUpload::go_upload($_FILES['test_detail']);
                    $uploadresult = '/uploads/'.LocalUpload::$filedir.'/'.LocalUpload::$filename;
                    $thumbs = LocalUpload::thumbs(100,100);
                }else{
                    $uploadresult = '';
                }
                $logicRes = $testLogic->addTest($params,$uploadresult);
            }elseif($params['test_type']==6){
                if($_FILES['test_detail']['name']){
                    LocalUpload::go_upload($_FILES['test_detail']);
                    $uploadresult = '/uploads/'.LocalUpload::$filedir.'/'.LocalUpload::$filename;
                    $thumbs = LocalUpload::thumbs(100,100);
                }else{
                    $uploadresult = '';
                }
                $logicRes = $testLogic->addTest($params,$uploadresult);
            }elseif($params['test_type']==7){
                if($_FILES['test_answer']['name']){
                    LocalUpload::go_upload_video($_FILES['test_answer']);
                    $uploadresult = '/uploads/'.LocalUpload::$filedir.'/'.LocalUpload::$filename;
                }else{
                    $uploadresult = '';
                }
                $logicRes = $testLogic->addTest($params,$uploadresult);
            }elseif($params['test_type']==8){
                $logicRes = $testLogic->addTest($params);
            }elseif($params['test_type']==9){
                $logicRes = $testLogic->addTest($params);
            }elseif($params['test_type']==10){
                if($_FILES['test_detail']['name']){
                    LocalUpload::go_upload($_FILES['test_detail']);
                    $uploadresult = '/uploads/'.LocalUpload::$filedir.'/'.LocalUpload::$filename;
                    $thumbs = LocalUpload::thumbs(100,100);
                }else{
                    $uploadresult = '';
                }
                $logicRes = $testLogic->addTest($params,$uploadresult);
            }elseif($params['test_type']==11){
                $logicRes = $testLogic->addTest($params);
            }elseif($params['test_type']==12){
                $logicRes = $testLogic->addTest($params);
            }elseif($params['test_type']==13){
                if($_FILES['test_options_A']['name']) {
                    LocalUpload::go_upload($_FILES['test_options_A']);
                    $uploadresult = '/uploads/' . LocalUpload::$filedir . '/' . LocalUpload::$filename;
                    $thumbs = LocalUpload::thumbs(100, 100);
                    $test_options['A'] = $uploadresult;
                    LocalUpload::go_upload($_FILES['test_options_B']);
                    $uploadresult = '/uploads/' . LocalUpload::$filedir . '/' . LocalUpload::$filename;
                    $thumbs = LocalUpload::thumbs(100, 100);
                    $test_options['B'] = $uploadresult;
                    LocalUpload::go_upload($_FILES['test_options_C']);
                    $uploadresult = '/uploads/' . LocalUpload::$filedir . '/' . LocalUpload::$filename;
                    $thumbs = LocalUpload::thumbs(100, 100);
                    $test_options['C'] = $uploadresult;
                    LocalUpload::go_upload($_FILES['test_options_D']);
                    $uploadresult = '/uploads/' . LocalUpload::$filedir . '/' . LocalUpload::$filename;
                    $thumbs = LocalUpload::thumbs(100, 100);
                    $test_options['D'] = $uploadresult;
                    $test_option = json_encode($test_options);
                }
                if($_FILES['test_detail']['name']){
                    LocalUpload::go_upload_video($_FILES['test_detail']);
                    $uploadresult = '/uploads/'.LocalUpload::$filedir.'/'.LocalUpload::$filename;
                }else{
                    $uploadresult = '';
                }
                $logicRes = $testLogic->addTest($params,$uploadresult,$test_option);
            }elseif($params['test_type']==14){
                if($_FILES['test_detail']['name']){
                    LocalUpload::go_upload_video($_FILES['test_detail']);
                    $uploadresult = '/uploads/'.LocalUpload::$filedir.'/'.LocalUpload::$filename;
                }else{
                    $uploadresult = '';
                }
                if($_FILES['test_extend']['name']){
                    LocalUpload::go_upload($_FILES['test_extend']);
                    $uploadresult1 = '/uploads/'.LocalUpload::$filedir.'/'.LocalUpload::$filename;
                    $thumbs = LocalUpload::thumbs(100,100);
                }else{
                    $uploadresult1 = '';
                }
                $logicRes = $testLogic->addTest($params,$uploadresult,$uploadresult1);
            }elseif($params['test_type']==15){
                if($_FILES['test_detail']['name']){
                    LocalUpload::go_upload($_FILES['test_detail']);
                    $uploadresult = '/uploads/'.LocalUpload::$filedir.'/'.LocalUpload::$filename;
                    $thumbs = LocalUpload::thumbs(100,100);
                }else{
                    $uploadresult = '';
                }
                $logicRes = $testLogic->addTest($params,$uploadresult);
            }elseif($params['test_type']==16){
                $logicRes = $testLogic->addTest($params);
            }elseif($params['test_type']==17){
                if($_FILES['test_detail']['name']){
                    LocalUpload::go_upload_video($_FILES['test_detail']);
                    $uploadresult = '/uploads/'.LocalUpload::$filedir.'/'.LocalUpload::$filename;
                }else{
                    $uploadresult = '';
                }
                $logicRes = $testLogic->addTest($params,$uploadresult);
            }elseif($params['test_type']==18){
                if($_FILES['test_detail']['name']){
                    LocalUpload::go_upload($_FILES['test_detail']);
                    $uploadresult = '/uploads/'.LocalUpload::$filedir.'/'.LocalUpload::$filename;
                    $thumbs = LocalUpload::thumbs(100,100);
                }else{
                    $uploadresult = '';
                }
                $logicRes = $testLogic->addTest($params,$uploadresult);
            }elseif($params['test_type']==19){
                if($_FILES['test_detail']['name']){
                    LocalUpload::go_upload($_FILES['test_detail']);
                    $uploadresult = '/uploads/'.LocalUpload::$filedir.'/'.LocalUpload::$filename;
                    $thumbs = LocalUpload::thumbs(100,100);
                }else{
                    $uploadresult = '';
                }
                $logicRes = $testLogic->addTest($params,$uploadresult);
            }elseif($params['test_type']==20){
                if($_FILES['test_detail']['name']){
                    LocalUpload::go_upload_video($_FILES['test_detail']);
                    $uploadresult = '/uploads/'.LocalUpload::$filedir.'/'.LocalUpload::$filename;
                }else{
                    $uploadresult = '';
                }
                $logicRes = $testLogic->addTest($params,$uploadresult);
            }elseif($params['test_type']==21){
                if($_FILES['test_extend']['name']){
                    LocalUpload::go_upload_video($_FILES['test_extend']);
                    $uploadresult = '/uploads/'.LocalUpload::$filedir.'/'.LocalUpload::$filename;
                }else{
                    $uploadresult = '';
                }
                $logicRes = $testLogic->addTest($params,$uploadresult);
            }elseif($params['test_type']==22){
                $logicRes = $testLogic->addTest($params);
            }elseif($params['test_type']==23){
                if($_FILES['test_options_A']['name']) {
                    LocalUpload::go_upload($_FILES['test_options_A']);
                    $uploadresult = '/uploads/' . LocalUpload::$filedir . '/' . LocalUpload::$filename;
                    $thumbs = LocalUpload::thumbs(100, 100);
                    $test_options['A'] = $uploadresult;
                    LocalUpload::go_upload($_FILES['test_options_B']);
                    $uploadresult = '/uploads/' . LocalUpload::$filedir . '/' . LocalUpload::$filename;
                    $thumbs = LocalUpload::thumbs(100, 100);
                    $test_options['B'] = $uploadresult;
                    LocalUpload::go_upload($_FILES['test_options_C']);
                    $uploadresult = '/uploads/' . LocalUpload::$filedir . '/' . LocalUpload::$filename;
                    $thumbs = LocalUpload::thumbs(100, 100);
                    $test_options['C'] = $uploadresult;
                    LocalUpload::go_upload($_FILES['test_options_D']);
                    $uploadresult = '/uploads/' . LocalUpload::$filedir . '/' . LocalUpload::$filename;
                    $thumbs = LocalUpload::thumbs(100, 100);
                    $test_options['D'] = $uploadresult;
                }
                if($_FILES['test_extend']['name']){
                    LocalUpload::go_upload_video($_FILES['test_extend']);
                    $uploadresult1 = '/uploads/'.LocalUpload::$filedir.'/'.LocalUpload::$filename;
                }else{
                    $uploadresult1 = '';
                }
                $logicRes = $testLogic->addTest($params,$uploadresult1,json_encode($test_options));
            }
            if ($logicRes) {
                Yii::$app->getSession()->setFlash('success','新增试题成功');
                if($params['paper_id']==1)
                    jump('test/chiness');
                elseif($params['paper_id']==2)
                    jump('test/spanish');
                else{
                    /*$paperLogic = new PaperLogic();
                    $paper = $paperLogic->getById($params['paper_id']);
                    if($paper['paper_type'])*/
                    jump(['paper/edit','paper_id'=>$params['paper_id']]);
                }

            } else {
                Yii::$app->getSession()->setFlash('error','新增试题失败');
                $this->goBack(Yii::$app->request->referrer);
            }
        }
    }
    //编辑试题
    public function actionEdit(){
        $testLogic = new TestLogic();
        if(!Yii::$app->request->post()){
            $id = Yii::$app->request->get('test_id');
            $paper_id = Yii::$app->request->get('paper_id');
            $test = $testLogic->getById($id);
            $test['paper_id'] = $paper_id;
            if($paper_id==1)
                $this->data['breadcrumbs'] = ['试题管理' => U('test/chiness'), $test['test_title'] => '', '更新' => ''];
            elseif($paper_id==2)
                $this->data['breadcrumbs'] = ['试题管理' => U('test/spanish'), $test['test_title'] => '', '更新' => ''];
            else{
                $paperLogic = new PaperLogic();
                $paper = $paperLogic->getById($paper_id);
                if($paper['paper_type']==3){
                    $this->data['breadcrumbs'] = ['试题管理' => U('paper/naughty'), '编辑模板' => U(['paper/edit','paper_id'=>$paper_id]), $test['test_title'] => '', '更新' => ''];
                }
                elseif($paper['paper_type']==4){
                    $this->data['breadcrumbs'] = ['试题管理' => U('paper/taotao'), '编辑模板' => U(['paper/edit','paper_id'=>$paper_id]), $test['test_title'] => '', '更新' => ''];
                }
                elseif($paper['paper_type']==5){
                    $this->data['breadcrumbs'] = ['试题管理' => U('paper/yct'), '编辑模板' => U(['paper/edit','paper_id'=>$paper_id]), $test['test_title'] => '', '更新' => ''];
                }
                elseif($paper['paper_type']==6){
                    $this->data['breadcrumbs'] = ['试题管理' => U('paper/hsk'), '编辑模板' => U(['paper/edit','paper_id'=>$paper_id]), $test['test_title'] => '', '更新' => ''];
                }
                elseif($paper['paper_type']==7){
                    $this->data['breadcrumbs'] = ['试题管理' => U('paper/pronunciation'), '编辑模板' => U(['paper/edit','paper_id'=>$paper_id]), $test['test_title'] => '', '更新' => ''];
                }
                elseif($paper['paper_type']==8){
                    $this->data['breadcrumbs'] = ['试题管理' => U('paper/children'), '编辑模板' => U(['paper/edit','paper_id'=>$paper_id]), $test['test_title'] => '', '更新' => ''];
                }
                elseif($paper['paper_type']==9){
                    $this->data['breadcrumbs'] = ['试题管理' => U('paper/dele'), '编辑模板' => U(['paper/edit','paper_id'=>$paper_id]), $test['test_title'] => '', '更新' => ''];
                }
            }
            if($test['test_type']==1){
                $test_options = json_decode($test['test_options'],true);
                return $this->render('edit_'.$test['test_type'], [
                    'test_options'=>isset($test_options)?$test_options:null,
                    'test' => $test,
                    'theme' => '/assets/',
                    'actionUrl' => U('test/edit'),
                ]);
            }elseif($test['test_type']==2){
                $test_options = json_decode($test['test_options'],true);
                return $this->render('edit_'.$test['test_type'], [
                    'test_options'=>isset($test_options)?$test_options:null,
                    'test' => $test,
                    'theme' => '/assets/',
                    'actionUrl' => U('test/edit'),
                ]);
            }elseif($test['test_type']==3){
                $test_options = json_decode($test['test_options'],true);
                return $this->render('edit_'.$test['test_type'], [
                    'test_options'=>isset($test_options)?$test_options:null,
                    'test' => $test,
                    'theme' => '/assets/',
                    'actionUrl' => U('test/edit'),
                ]);
            }elseif($test['test_type']==4){
                $test_options = json_decode($test['test_options'],true);
                return $this->render('edit_'.$test['test_type'], [
                    'test_options'=>isset($test_options)?$test_options:null,
                    'test' => $test,
                    'theme' => '/assets/',
                    'actionUrl' => U('test/edit'),
                ]);
            }elseif($test['test_type']==5){
                $test_options = json_decode($test['test_options'],true);
                return $this->render('edit_'.$test['test_type'], [
                    'test_options'=>isset($test_options)?$test_options:null,
                    'test' => $test,
                    'theme' => '/assets/',
                    'actionUrl' => U('test/edit'),
                ]);
            }elseif($test['test_type']==6){
                $test_options = json_decode($test['test_options'],true);
                return $this->render('edit_'.$test['test_type'], [
                    'test_options'=>isset($test_options)?$test_options:null,
                    'test' => $test,
                    'theme' => '/assets/',
                    'actionUrl' => U('test/edit'),
                ]);
            }elseif($test['test_type']==7){
                return $this->render('edit_'.$test['test_type'], [
                    'test' => $test,
                    'theme' => '/assets/',
                    'actionUrl' => U('test/edit'),
                ]);
            }elseif($test['test_type']==8){
                $test_options = json_decode($test['test_options'],true);
                return $this->render('edit_'.$test['test_type'], [
                    'test_options'=>isset($test_options)?$test_options:null,
                    'test' => $test,
                    'theme' => '/assets/',
                    'actionUrl' => U('test/edit'),
                ]);
            }elseif($test['test_type']==9){
                $test_options = json_decode($test['test_options'],true);
                return $this->render('edit_'.$test['test_type'], [
                    'test_options'=>isset($test_options)?$test_options:null,
                    'test' => $test,
                    'theme' => '/assets/',
                    'actionUrl' => U('test/edit'),
                ]);
            }elseif($test['test_type']==10){
                return $this->render('edit_'.$test['test_type'], [
                    'test' => $test,
                    'theme' => '/assets/',
                    'actionUrl' => U('test/edit'),
                ]);
            }elseif($test['test_type']==11){
                $test_options = json_decode($test['test_options'],true);
                return $this->render('edit_'.$test['test_type'], [
                    'test_options'=>isset($test_options)?$test_options:null,
                    'test' => $test,
                    'theme' => '/assets/',
                    'actionUrl' => U('test/edit'),
                ]);
            }elseif($test['test_type']==12){
                $test_options = json_decode($test['test_options'],true);
                return $this->render('edit_'.$test['test_type'], [
                    'test_options'=>isset($test_options)?$test_options:null,
                    'test' => $test,
                    'theme' => '/assets/',
                    'actionUrl' => U('test/edit'),
                ]);
            }elseif($test['test_type']==13){
                $test_options = json_decode($test['test_options'],true);
                return $this->render('edit_'.$test['test_type'], [
                    'test_options'=>isset($test_options)?$test_options:null,
                    'test' => $test,
                    'theme' => '/assets/',
                    'actionUrl' => U('test/edit'),
                ]);
            }elseif($test['test_type']==14){
                $test_options = json_decode($test['test_options'],true);
                return $this->render('edit_'.$test['test_type'], [
                    'test_options'=>isset($test_options)?$test_options:null,
                    'test' => $test,
                    'theme' => '/assets/',
                    'actionUrl' => U('test/edit'),
                ]);
            }elseif($test['test_type']==15){
                $test_options = json_decode($test['test_options'],true);
                return $this->render('edit_'.$test['test_type'], [
                    'test_options'=>isset($test_options)?$test_options:null,
                    'test' => $test,
                    'theme' => '/assets/',
                    'actionUrl' => U('test/edit'),
                ]);
            }elseif($test['test_type']==16){
                $test_options = json_decode($test['test_options'],true);
                return $this->render('edit_'.$test['test_type'], [
                    'test_options'=>isset($test_options)?$test_options:null,
                    'test' => $test,
                    'theme' => '/assets/',
                    'actionUrl' => U('test/edit'),
                ]);
            }elseif($test['test_type']==17){
                $test_options = json_decode($test['test_options'],true);
                return $this->render('edit_'.$test['test_type'], [
                    'test_options'=>isset($test_options)?$test_options:null,
                    'test' => $test,
                    'theme' => '/assets/',
                    'actionUrl' => U('test/edit'),
                ]);
            }elseif($test['test_type']==18){
                $test_options = json_decode($test['test_options'],true);
                return $this->render('edit_'.$test['test_type'], [
                    'test_options'=>isset($test_options)?$test_options:null,
                    'test' => $test,
                    'theme' => '/assets/',
                    'actionUrl' => U('test/edit'),
                ]);
            }elseif($test['test_type']==19){
                $test_options = json_decode($test['test_options'],true);
                return $this->render('edit_'.$test['test_type'], [
                    'test_options'=>isset($test_options)?$test_options:null,
                    'test' => $test,
                    'theme' => '/assets/',
                    'actionUrl' => U('test/edit'),
                ]);
            }elseif($test['test_type']==20){
                return $this->render('edit_'.$test['test_type'], [
                    'test' => $test,
                    'theme' => '/assets/',
                    'actionUrl' => U('test/edit'),
                ]);
            }elseif($test['test_type']==21){
                $test_options = json_decode($test['test_options'],true);
                return $this->render('edit_'.$test['test_type'], [
                    'test_options'=>isset($test_options)?$test_options:null,
                    'test' => $test,
                    'theme' => '/assets/',
                    'actionUrl' => U('test/edit'),
                ]);
            }elseif($test['test_type']==22){
                $test_options = json_decode($test['test_options'],true);
                return $this->render('edit_'.$test['test_type'], [
                    'test_options'=>isset($test_options)?$test_options:null,
                    'test' => $test,
                    'theme' => '/assets/',
                    'actionUrl' => U('test/edit'),
                ]);
            }elseif($test['test_type']==23){
                $test_options = json_decode($test['test_options'],true);
                return $this->render('edit_'.$test['test_type'], [
                    'test_options'=>isset($test_options)?$test_options:null,
                    'test' => $test,
                    'theme' => '/assets/',
                    'actionUrl' => U('test/edit'),
                ]);
            }

        }else{
            set_time_limit(0);
            $params = Yii::$app->request->post('form');
            $id = Yii::$app->request->post('test_id');
            $test = $testLogic->getById($id);
            if($test['test_type']==1){
                if($_FILES['test_detail']['name']){
                    LocalUpload::go_upload_video($_FILES['test_detail']);
                    $uploadresult = '/uploads/'.LocalUpload::$filedir.'/'.LocalUpload::$filename;
                }else{
                    $uploadresult = '';
                }
                $logicRes = $testLogic->editById($id,$params,$uploadresult);
            }elseif($params['test_type']==2){
                $logicRes = $testLogic->editById($id,$params);
            }elseif($params['test_type']==3){
                $logicRes = $testLogic->editById($id,$params);
            }elseif($params['test_type']==4){
                $test_options = null;
                if($_FILES['test_options_A']['name']){
                    LocalUpload::go_upload($_FILES['test_options_A']);
                    $uploadresult = '/uploads/'.LocalUpload::$filedir.'/'.LocalUpload::$filename;
                    $thumbs = LocalUpload::thumbs(100,100);
                    $test_options['A'] = $uploadresult;
                }
                if($_FILES['test_options_B']['name']){
                    LocalUpload::go_upload($_FILES['test_options_B']);
                    $uploadresult = '/uploads/'.LocalUpload::$filedir.'/'.LocalUpload::$filename;
                    $thumbs = LocalUpload::thumbs(100,100);
                    $test_options['B'] = $uploadresult;
                }
                if($_FILES['test_options_C']['name']){
                    LocalUpload::go_upload($_FILES['test_options_C']);
                    $uploadresult = '/uploads/'.LocalUpload::$filedir.'/'.LocalUpload::$filename;
                    $thumbs = LocalUpload::thumbs(100,100);
                    $test_options['C'] = $uploadresult;
                }
                if($_FILES['test_options_D']['name']){
                    LocalUpload::go_upload($_FILES['test_options_D']);
                    $uploadresult = '/uploads/'.LocalUpload::$filedir.'/'.LocalUpload::$filename;
                    $thumbs = LocalUpload::thumbs(100,100);
                    $test_options['D'] = $uploadresult;
                }
                $logicRes = $testLogic->editById($id,$params,$test_options);
            }elseif($params['test_type']==5){
                if($_FILES['test_detail']['name']){
                    LocalUpload::go_upload($_FILES['test_detail']);
                    $uploadresult = '/uploads/'.LocalUpload::$filedir.'/'.LocalUpload::$filename;
                    $thumbs = LocalUpload::thumbs(100,100);
                }else{
                    $uploadresult = '';
                }
                $logicRes = $testLogic->editById($id,$params,$uploadresult);
            }elseif($params['test_type']==6){
                if($_FILES['test_detail']['name']){
                    LocalUpload::go_upload($_FILES['test_detail']);
                    $uploadresult = '/uploads/'.LocalUpload::$filedir.'/'.LocalUpload::$filename;
                    $thumbs = LocalUpload::thumbs(100,100);
                }else{
                    $uploadresult = '';
                }
                $logicRes = $testLogic->editById($id,$params,$uploadresult);
            }elseif($params['test_type']==7){
                if($_FILES['test_answer']['name']){
                    LocalUpload::go_upload_video($_FILES['test_answer']);
                    $uploadresult = '/uploads/'.LocalUpload::$filedir.'/'.LocalUpload::$filename;
                }else{
                    $uploadresult = '';
                }
                $logicRes = $testLogic->editById($id,$params,$uploadresult);
            }elseif($params['test_type']==8){
                $logicRes = $testLogic->editById($id,$params);
            }elseif($params['test_type']==9){
                $logicRes = $testLogic->editById($id,$params);
            }elseif($params['test_type']==10){
                if($_FILES['test_detail']['name']){
                    LocalUpload::go_upload($_FILES['test_detail']);
                    $uploadresult = '/uploads/'.LocalUpload::$filedir.'/'.LocalUpload::$filename;
                    $thumbs = LocalUpload::thumbs(100,100);
                }else{
                    $uploadresult = '';
                }
                $logicRes = $testLogic->editById($id,$params,$uploadresult);
            }elseif($params['test_type']==11){
                $logicRes = $testLogic->editById($id,$params);
            }elseif($params['test_type']==12){
                $logicRes = $testLogic->editById($id,$params);
            }elseif($params['test_type']==13){
                $test_options = null;
                if($_FILES['test_options_A']['name']){
                    LocalUpload::go_upload($_FILES['test_options_A']);
                    $uploadresult = '/uploads/'.LocalUpload::$filedir.'/'.LocalUpload::$filename;
                    $thumbs = LocalUpload::thumbs(100,100);
                    $test_options['A'] = $uploadresult;
                }
                if($_FILES['test_options_B']['name']){
                    LocalUpload::go_upload($_FILES['test_options_B']);
                    $uploadresult = '/uploads/'.LocalUpload::$filedir.'/'.LocalUpload::$filename;
                    $thumbs = LocalUpload::thumbs(100,100);
                    $test_options['B'] = $uploadresult;
                }
                if($_FILES['test_options_C']['name']){
                    LocalUpload::go_upload($_FILES['test_options_C']);
                    $uploadresult = '/uploads/'.LocalUpload::$filedir.'/'.LocalUpload::$filename;
                    $thumbs = LocalUpload::thumbs(100,100);
                    $test_options['C'] = $uploadresult;
                }
                if($_FILES['test_options_D']['name']){
                    LocalUpload::go_upload($_FILES['test_options_D']);
                    $uploadresult = '/uploads/'.LocalUpload::$filedir.'/'.LocalUpload::$filename;
                    $thumbs = LocalUpload::thumbs(100,100);
                    $test_options['D'] = $uploadresult;
                }
                if($_FILES['test_detail']['name']){
                    LocalUpload::go_upload_video($_FILES['test_detail']);
                    $uploadresult = '/uploads/'.LocalUpload::$filedir.'/'.LocalUpload::$filename;
                }else{
                    $uploadresult = '';
                }
                $logicRes = $testLogic->editById($id,$params,$uploadresult,$test_options);
            }elseif($params['test_type']==14){
                if($_FILES['test_detail']['name']){
                    LocalUpload::go_upload_video($_FILES['test_detail']);
                    $uploadresult = '/uploads/'.LocalUpload::$filedir.'/'.LocalUpload::$filename;
                }else{
                    $uploadresult = '';
                }
                if($_FILES['test_extend']['name']){
                    LocalUpload::go_upload($_FILES['test_extend']);
                    $uploadresult1 = '/uploads/'.LocalUpload::$filedir.'/'.LocalUpload::$filename;
                    $thumbs = LocalUpload::thumbs(100,100);
                }else{
                    $uploadresult1 = '';
                }
                $logicRes = $testLogic->editById($id,$params,$uploadresult,$uploadresult1);
            }elseif($params['test_type']==15){
                if($_FILES['test_detail']['name']){
                    LocalUpload::go_upload($_FILES['test_detail']);
                    $uploadresult = '/uploads/'.LocalUpload::$filedir.'/'.LocalUpload::$filename;
                    $thumbs = LocalUpload::thumbs(100,100);
                }else{
                    $uploadresult = '';
                }
                $logicRes = $testLogic->editById($id,$params,$uploadresult);
            }elseif($params['test_type']==16){
                $logicRes = $testLogic->editById($id,$params);
            }elseif($params['test_type']==17){
                if($_FILES['test_detail']['name']){
                    LocalUpload::go_upload_video($_FILES['test_detail']);
                    $uploadresult = '/uploads/'.LocalUpload::$filedir.'/'.LocalUpload::$filename;
                }else{
                    $uploadresult = '';
                }
                $logicRes = $testLogic->editById($id,$params,$uploadresult);
            }elseif($params['test_type']==18){
                if($_FILES['test_detail']['name']){
                    LocalUpload::go_upload($_FILES['test_detail']);
                    $uploadresult = '/uploads/'.LocalUpload::$filedir.'/'.LocalUpload::$filename;
                    $thumbs = LocalUpload::thumbs(100,100);
                }else{
                    $uploadresult = '';
                }
                $logicRes = $testLogic->editById($id,$params,$uploadresult);
            }elseif($params['test_type']==19){
                if($_FILES['test_detail']['name']){
                    LocalUpload::go_upload($_FILES['test_detail']);
                    $uploadresult = '/uploads/'.LocalUpload::$filedir.'/'.LocalUpload::$filename;
                    $thumbs = LocalUpload::thumbs(100,100);
                }else{
                    $uploadresult = '';
                }
                $logicRes = $testLogic->editById($id,$params,$uploadresult);
            }elseif($params['test_type']==20){
                if($_FILES['test_detail']['name']){
                    LocalUpload::go_upload_video($_FILES['test_detail']);
                    $uploadresult = '/uploads/'.LocalUpload::$filedir.'/'.LocalUpload::$filename;
                }else{
                    $uploadresult = '';
                }
                $logicRes = $testLogic->editById($id,$params,$uploadresult);
            }elseif($params['test_type']==21){
                if($_FILES['test_extend']['name']){
                    LocalUpload::go_upload_video($_FILES['test_extend']);
                    $uploadresult = '/uploads/'.LocalUpload::$filedir.'/'.LocalUpload::$filename;
                }else{
                    $uploadresult = '';
                }
                $logicRes = $testLogic->editById($id,$params,$uploadresult);
            }elseif($params['test_type']==22){
                $logicRes = $testLogic->editById($id,$params);
            }elseif($params['test_type']==23){
                $test_options = null;
                if($_FILES['test_options_A']['name']){
                    LocalUpload::go_upload($_FILES['test_options_A']);
                    $uploadresult = '/uploads/'.LocalUpload::$filedir.'/'.LocalUpload::$filename;
                    $thumbs = LocalUpload::thumbs(100,100);
                    $test_options['A'] = $uploadresult;
                }
                if($_FILES['test_options_B']['name']){
                    LocalUpload::go_upload($_FILES['test_options_B']);
                    $uploadresult = '/uploads/'.LocalUpload::$filedir.'/'.LocalUpload::$filename;
                    $thumbs = LocalUpload::thumbs(100,100);
                    $test_options['B'] = $uploadresult;
                }
                if($_FILES['test_options_C']['name']){
                    LocalUpload::go_upload($_FILES['test_options_C']);
                    $uploadresult = '/uploads/'.LocalUpload::$filedir.'/'.LocalUpload::$filename;
                    $thumbs = LocalUpload::thumbs(100,100);
                    $test_options['C'] = $uploadresult;
                }
                if($_FILES['test_options_D']['name']){
                    LocalUpload::go_upload($_FILES['test_options_D']);
                    $uploadresult = '/uploads/'.LocalUpload::$filedir.'/'.LocalUpload::$filename;
                    $thumbs = LocalUpload::thumbs(100,100);
                    $test_options['D'] = $uploadresult;
                }
                if($_FILES['test_extend']['name']){
                    LocalUpload::go_upload_video($_FILES['test_extend']);
                    $uploadresult1 = '/uploads/'.LocalUpload::$filedir.'/'.LocalUpload::$filename;
                }else{
                    $uploadresult1 = '';
                }
                $logicRes = $testLogic->editById($id,$params,$uploadresult1,$test_options);
            }


            if ($logicRes) {
                Yii::$app->getSession()->setFlash('success','更新试题成功');
                if($params['paper_id']==1)
                    jump('test/chiness');
                elseif($params['paper_id']==2)
                    jump('test/spanish');
                else{
                    /*$paperLogic = new PaperLogic();
                    $paper = $paperLogic->getById($params['paper_id']);
                    if($paper['paper_type'])*/
                    jump(['paper/edit','paper_id'=>$params['paper_id']]);
                }


            } else {
                Yii::$app->getSession()->setFlash('error','更新试题失败');
                $this->goBack(Yii::$app->request->referrer);
            }
        }
    }
    //删除试题
    public function actionDelete(){
        $id = Yii::$app->request->get('test_id');
        $paper_id = Yii::$app->request->get('paper_id');
        $testLogic = new TestLogic();
        $test = $testLogic->getById($id);
        $logicRes = $testLogic->delete($id,$paper_id);
        if ($logicRes) {
            Yii::$app->getSession()->setFlash('success','删除成功');
        } else {
            Yii::$app->getSession()->setFlash('error','删除失败');
        }
        if($paper_id==1)
            return $this->redirect(U('test/chiness'));
        elseif($paper_id==2)
            return $this->redirect(U('test/spanish'));
        else{
            return $this->redirect(U(['paper/edit','paper_id'=>$paper_id]));
        }
    }
    //试题排序
    public function actionSortTest(){
        $id = Yii::$app->request->post('test_id');
        $paper_id = Yii::$app->request->post('paper_id');
        $sort = Yii::$app->request->post('sort');
        $testLogic = new TestLogic();
        $logicRes = $testLogic->sortTest($id,$paper_id,$sort);
        echo json_encode($logicRes);
    }


}
